const app = getApp();
const pageSize = 15;

const statusOptions = [{
  value: '',
  name: '全部'
}, {
  value: 1,
  name: '正常'
}, {
  value: 2,
  name: '禁用'
}];

const selfOptions = [{
  value: '',
  name: '全部'
}, {
  value: 1,
  name: '自营'
}, {
  value: 2,
  name: '非自营'
}]
Page({
  data: {
    selfOptions,
    statusOptions,
    empty: false,
    isFilter: false,
    nomore: false,
    loadmore: false,
    page: 1, //当前页数
    drawer: false,
    list: [],
    autarky: 0, //自营人数
    unautarky: 0, //非自营人数
    total: 0, // 总人数
    conditions: {
      startTime: "",
      endTime: '',
      keyword: "",
      selfIndex: '',
      statusIndex: '',
    },
  },

  onLoad: function () {
    this.getFranchiserList();
  },




  reloadData() {
    this.setData({
      page: 1,
      list: [],
      nomore: false,
      loadmore: false,
      total: 0,
      autarky: 0,
      unautarky: 0
    })
    wx.showLoading();
    this.getFranchiserList();
  },


  getFranchiserList() {
    let that = this;
    app.ajax({
      url: 'Partner/Franchiser/franchiserList',
      data: {
        page: that.data.page,
        row: pageSize,
        start_time: that.data.conditions.startTime,
        end_time: that.data.conditions.endTime,
        account_status: that.data.conditions.statusIndex !== "" ? that.data.statusOptions[that.data.conditions.statusIndex].value : '',
        keyword: that.data.conditions.keyword,
        is_self: that.data.conditions.selfIndex !== "" ? that.data.selfOptions[that.data.conditions.selfIndex].value : '',
        is_page: 1, //传递本参数有分页内容
      },
      success: function (res) {
        wx.hideLoading()
        if (res.data.code > 1000) {
          console.log(res.data.data);
          const source = res.data.data;
          let list = that.data.list.concat(source.list);
          that.setData({
            list,
            total: source.count,
            unautarky: source.no_self_count,
            autarky: source.self_count,
            empty: source.page.page_count == 0 ? true : false,
            loadmore: list.length < source.count ? true : false,
            nomore: list.length >= source.count ? true : false,
            drawer: false,
          })
        }
      },
      error: e => {
        wx.hideLoading()
      }
    })
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.getFranchiserList();
    }
  },

  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },



  handleCondition(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },


  handleChange(e) {
    const {
      name,
      index
    } = e.currentTarget.dataset;
    this.setData({
      [`conditions.${name}`]: index
    })
  },

  //添加加盟商
  addFranchiser(e) {
    console.info(e);
    wx.navigateTo({
      url: '../franchiseeeListEdit/franchiseeeListEdit?type=1',
    })
  },
  //编辑加盟商
  editFranchiseee(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../franchiseeeListEdit/franchiseeeListEdit?type=2&id=' + id,
    })
  },
  //查看加盟商
  lookFranchiseee(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../franchiseeeListEdit/franchiseeeListEdit?type=3&id=' + id,
    })
  },
  //删除加盟商
  deleteFranchiseee(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      content: '是否删除加盟商?',
      confirmText: '是',
      cancelText: '否',
      success: function (res) {
        //console.log(res)
        if (res.confirm) {
          wx.hideLoading();
          app.ajax({
            url: 'Partner/Franchiser/delFranchiser',
            data: {
              id: id,
            },
            success: function (res) {
              if (res.data.code == 5000) {
                app.showToast(res.data.message);
                that.reloadData();
              } else {
                app.showToast(res.data.message);
                return false;
              }
            }
          })
        }
      },
    })
  }


})